package com.mars.module.tool.controller;

import com.mars.common.enums.BusinessType;
import com.mars.common.request.tool.GenCodeDeployRequest;
import com.mars.common.request.tool.GenCodeRemoveRequest;
import com.mars.common.response.PageInfo;
import com.mars.common.response.tool.TableListResponse;
import com.mars.common.result.R;
import com.mars.common.request.tool.TableQueryRequest;
import com.mars.framework.annotation.Log;
import com.mars.framework.exception.ServiceException;
import com.mars.module.tool.entity.GenTable;
import com.mars.module.tool.request.CreateTableRequest;
import com.mars.module.tool.request.GenTableImportRequest;
import com.mars.module.tool.request.GenTableRequest;
import com.mars.module.tool.response.GenTableResponse;
import com.mars.module.tool.service.ITableGenerationService;
import com.mars.module.tool.service.IDatabaseTableService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import lombok.AllArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.sql.SQLException;
import java.util.List;

/**
 * 代码生成控制器
 *
 * @author 程序员Mars
 */
@Api(tags = "工具管理-代码生成控制器")
@RestController
@RequestMapping("/tool/table")
@AllArgsConstructor
public class CodeGenerationController {

    private final IDatabaseTableService tableService;

    private final ITableGenerationService genTableService;


    /**
     * 查询已导入表列表
     *
     * @param request request
     * @return R
     */
    @PostMapping("/genTableList")
    @ApiOperation(value = "查询已导入表列表")
    public R<PageInfo<GenTableResponse>> genTableList(@RequestBody GenTableRequest request) {
        return R.success(genTableService.genTableList(request));
    }


    /**
     * 获取数据库表列表
     *
     * @param request request
     * @return R
     */
    @PostMapping("/tableList")
    @ApiOperation(value = "获取表列表")
    public R<PageInfo<TableListResponse>> list(@RequestBody TableQueryRequest request) {
        return R.success(tableService.list(request));
    }

    /**
     * 删除已导入表
     *
     * @param ids ids
     * @return R
     */
    @Log(title = "删除已导入表", businessType = BusinessType.DELETE)
    @ApiOperation(value = "删除已导入表")
    @PostMapping("/delete/{ids}")
    public R<Void> delete(@PathVariable Long[] ids) {
        if (ids.length == 0) {
            throw new ServiceException("请选择删除导入表结构");
        }
        for (Long id : ids) {
            genTableService.deleteGenTableById(id);
        }
        return R.success();
    }


    /**
     * 已导入表 详情
     *
     * @param id id
     * @return R
     */
    @ApiOperation(value = "导入表详情")
    @GetMapping("/detail/{id}")
    public R<GenTableResponse> detail(@PathVariable("id") Long id) {
        return R.success(genTableService.detail(id));
    }

    /**
     * 已导入表 更新
     *
     * @param genTable genTable
     * @return R
     */
    @Log(title = "更新已导入表", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "已导入表 更新")
    @PostMapping("/update")
    public R<Void> updateGenTable(@RequestBody GenTable genTable) {
        genTableService.updateGenTable(genTable);
        return R.success();
    }

    /**
     * 生成代码+一键部署
     *
     * @param request 请求参数
     * @return 数据
     */
    @Log(title = "一键部署", businessType = BusinessType.INSERT)
    @PostMapping("/deploy")
    public R<Void> deploy(@RequestBody GenCodeDeployRequest request) {
        genTableService.deploy(request);
        return R.success();
    }

    /**
     * 卸载一键部署的内容
     *
     * @param request 请求参数
     * @return 数据
     */
    @Log(title = "一键卸载", businessType = BusinessType.DELETE)
    @ApiOperation(value = "卸载一键部署的内容")
    @PostMapping("/deployRemove")
    public R<Void> deployRemove(@RequestBody List<GenCodeRemoveRequest> request) {
        if (CollectionUtils.isEmpty(request)) {
            throw new ServiceException("请选择需卸载表结构");
        }
        for (GenCodeRemoveRequest removeRequest : request) {
            genTableService.remove(removeRequest);
        }
        return R.success();
    }


    /**
     * 导入表结构（保存）
     */
    @Log(title = "导入表结构", businessType = BusinessType.INSERT)
    @ApiOperation(value = "导入表结构")
    @PostMapping("/importTable")
    public R<Void> importTableSave(@RequestBody GenTableImportRequest request) {
        genTableService.importGenTable(request);
        return R.success();
    }


    /**
     * 批量一键部署
     *
     * @param request 请求参数
     * @return 数据
     */
    @Log(title = "批量一键部署", businessType = BusinessType.INSERT)
    @PostMapping("/batchDeploy")
    public R<Void> batchDeploy(@RequestBody List<GenCodeDeployRequest> request) {
        if (CollectionUtils.isEmpty(request)) {
            throw new ServiceException("请选择部署表结构");
        }
        for (GenCodeDeployRequest genCodeDeployRequest : request) {
            genTableService.deploy(genCodeDeployRequest);
        }
        return R.success();
    }


    /**
     * 一键建表
     */
    @Log(title = "一键建表", businessType = BusinessType.INSERT)
    @ApiOperation(value = "一键建表")
    @PostMapping("/createTable")
    public R<Void> createTable(@RequestBody CreateTableRequest request) throws SQLException {
        genTableService.createTable(request);
        return R.success();
    }


}
